<?php
/**
 * 核心文件
 *
 * 核心初始化类，不允许继承
 *
 */

final class Base{

    const CPURL = '';

    /**
     * init
     */
    public static function init() {
       // var_dump($GLOBALS['config']);die;
        //设置配置文件
        @date_default_timezone_set('Asia/Shanghai');
        //Model3::set_link('meirong','m_');//设置数据库
        //加载基础配置文件，并配置数据库相关等属性
        Model3::set_link($GLOBALS['config']['db']['master']['dbname'],$GLOBALS['config']['db']['master']['dbprefix']);//设置数据库
    }

    /**
     * run
     */
    public static function run(){
      //  self::cp();//dfadsasdfads
        self::init();

        //if(APP_ID=='admin'&&AdminHandleCheck=='on'){
        //    self::authAdminCheck();
        //}
        //这里做一步权限验证
       // self:: authority_check();
        if(isset($GLOBALS['type'])&&$GLOBALS['type']=='control')
        {
            self::control(); //编译模板不需要开启session
        }
        else{
            self::start_session();
            self::service();
        }

    }


    /**
     * 控制器调度serive调度
     *
     */
    private static function service(){
        include(BASE_PATH.'/service/service.php');
        $act_file =BASE_PATH.'/service/'.$_GET['act'].'.php';
        $class_name = $_GET['act'].'Service';
        if (!@include($act_file)){
                exit("Base Error: $act_file access file isn't exists!");
        }

        if (class_exists($class_name)){
            $main = new $class_name();
            $function = $_GET['op'];

            if (method_exists($main,$function)){
                $main->$function();
            }else {
                $error = "Base Error: function $function not in $class_name!";
                exit($error);
            }
        }else {
            $error = "Base Error: class $class_name isn't exists!";
            exit($error);
        }
    }
  /**
  * controller 调度
   */
    private  static  function  control(){
         include(BASE_PATH.'/control/control.php');
        $act_file =BASE_PATH.'/control/'.$_GET['act'].'.php';
        $class_name = $_GET['act'].'Control';
        if (!@include($act_file)){
            exit("Base Error: $act_file access file isn't exists!");
        }

        if (class_exists($class_name)){
            $main = new $class_name();
            $function = $_GET['op'];

            if (method_exists($main,$function)){
                $main->$function();
            }else {
                $error = "Base Error: function $function not in $class_name!";
                exit($error);
            }
        }else {
            $error = "Base Error: class $class_name isn't exists!";
            exit($error);
        }
    }


    //访问权限控制
   public  static  function  authority_check(){

      // if(APP_ID!='shop'){return;}
       header("Content-type: text/html; charset=utf-8");
       $control=$_GET['act'];
       $op=$_GET['op'];
      //检测当前用户登陆情况
      if(strstr($control,'seller')&&$control!='seller_login'){
        if($_SESSION['is_seller_login']){
            //检测用户id对应的  权限情况，查找角色，角色包含指定的control和op 包含的control和control下的op
             $member_id=$_SESSION['admin_id'];
            $auth_path=BASE_DATA_PATH.'/authority/role.php';
             $role= include($auth_path);
            $auth_path=BASE_DATA_PATH.'/authority/member.php';
            $member= include ($auth_path);
            $menu_path=BASE_DATA_PATH.'/authority/menu.php';
            $auth_menu= include( $menu_path);
            $authority_menu=$auth_menu[$member[$member_id]];//获取用户的角色权限第一个


            $authority=$role[$member[$member_id]]['control'];//获取用户的角色权限

            if(!$authority){exit('请先设置用户权限');}
            if(!$authority_menu){exit('请先设置用户菜单');}
            //检测当前control和op是否包含于其中
            if(array_key_exists($control,$authority)){
                 if(!in_array($op,$authority[$control])){
                  exit('没有方法权限');
                    //跳转到默认菜单
                   // $url='index.php?act='.$authority_menu[0]['act'].'&op='.$authority_menu[0]['op'];
                  // @header("location: $url");
                 }
            }
            else{
              //  $url='index.php?act='.$authority_menu[0]['act'].'&op='.$authority_menu[0]['op'];
              //  @header("location: $url");
                exit('没有模块权限');
               // @header('location: index.php?act=seller_login&wrods=没有模块权限');
            }
        }
            else{
                @header('location: index.php?act=seller_login');
            }
      }
   }
    /**
     * 开启session
     *
     */
    public  static function start_session(){

  //暂时不考虑二级域名
        // var_dump(SUBDOMAIN_SUFFIX) ;die; string 0

       $domain= explode(':',$_SERVER['HTTP_HOST']);

        // $subdomain_suffix;die; 子域名后缀124.21.206:8090
        //session.name强制定制成PHPSESSID,不请允许更改
        @ini_set('session.name','PHPSESSID');
       // $subdomain_suffix = str_replace('http://','',$domain[0]);

        if ($domain[0] !== 'localhost') { //不同域的cookie
                 $domain_name=$GLOBALS['config']['domain_name'];
                @ini_set('session.cookie_domain', $domain_name);//
        }

        //开启以下配置支持session信息存信memcache
        if('cachetype'==='memcache'){
            @ini_set("session.save_handler", "memcache");
            @ini_set("session.save_path", C('memcache.1.host').':'.C('memcache.1.port'));
        }
        else{
            $path=BASE_DATA_PATH.'/cache/session';
            if(!is_dir($path)){mk_dir($path,0777,true);}
            session_save_path($path); //去掉文件形式路径
        }
        session_start(); //数据输出到浏览器之前调用
    }

    //主要用于自动注册函数库类的文件 在framework/libraries下面
    public static function autoload($class){
        if (!@include(ROOT_PATH.'/lib/'.$class.'.php')){
            exit("Class Error::【{$class}】isn't exists!");
        }
    }

}